//
//  WBVistorView.swift
//  simpleWeibo
//
//  Created by 孙林 on 2017/12/17.
//  Copyright © 2017年 孙林. All rights reserved.
//

import UIKit

class WBVistorView: UIView {
    //注册按钮
    lazy var registerButton : UIButton = UIButton.cz_textButton("注册", fontSize: 16,
                                                                normalColor: UIColor.orange,
                                                                highlightedColor: UIColor.darkGray,
                                                                backgroundImageName: "common_button_white_disable")
    //登录按钮
    lazy var loginButton : UIButton = UIButton.cz_textButton("登录",
                                                             fontSize: 16,
                                                             normalColor: UIColor.darkGray,
                                                             highlightedColor: UIColor.darkGray,
                                                             backgroundImageName: "common_button_white_disable")
    
    var visitorInfo : [String : String]? {
        didSet {
            //取字典信息
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"]
            else {
                return
            }
            if imageName == "" {
                startAnimation()
                return
            }
            iconImageView.image = UIImage(named: imageName)
            tipLab.text = message
            houseImageView.isHidden = true
            maskIconView.isHidden = true
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        //动画完成不删除, 如果iconImageView被销毁,动画会一起销毁
        animation.isRemovedOnCompletion = false
        iconImageView.layer.add(animation, forKey: nil)
    }
    
    // MARK: - 私有控件
    //图像视图
    fileprivate lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩视图
    fileprivate lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    fileprivate lazy var houseImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示标签
    fileprivate lazy var tipLab : UILabel = UILabel.cz_label(withText: "关注一些人, 回这里看看有什么惊喜;关注一些人, 回这里看看有什么惊喜;", fontSize: 14, color: UIColor.darkGray, textAlignment: .center)

}

extension WBVistorView {
    func setupUI() {
        let margin : CGFloat = 20.0
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        //添加
        addSubview(iconImageView)
        insertSubview(maskIconView, aboveSubview: iconImageView)
        addSubview(houseImageView)
        addSubview(tipLab)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
            
        }
        
        //自动布局
        //大圈子
        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        //遮罩视图
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .left,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .top,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .right,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView,
                                         attribute: .bottom,
                                         relatedBy: .equal,
                                         toItem: tipLab,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: margin))
        //房子
        addConstraint(NSLayoutConstraint(item: houseImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        //文字介绍
        addConstraint(NSLayoutConstraint(item: tipLab,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLab,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconImageView,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLab,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1,
                                         constant: 236))
        //注册
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLab,
                                         attribute: .left,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLab,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1,
                                         constant: 100))
        //登录
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLab,
                                         attribute: .right,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLab,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1,
                                         constant: 100))
    }
    
    
}
